package com.qdsgjsfk.vision.ui.fragment;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qdsgjsfk.vision.R;
import com.qdsgjsfk.vision.databinding.FragmentCollectClassListBinding;

import com.qdsgjsfk.vision.adapter.CollectClassAdapter;
import com.qdsgjsfk.vision.viewmodel.CollectClassViewModel;
import com.rest.response.CollectClassResponse;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * Created by HP on 2019/11/27.
 */

public class CollectClassFragment extends Fragment {

    FragmentCollectClassListBinding mBinding;
    CollectClassViewModel collectClassViewModel;
    List<CollectClassResponse.Class> list = new ArrayList<>();

    public static CollectClassFragment newInstance(String type, String taskId) {
        Bundle args = new Bundle();
        args.putString("type", type);
        args.putString("taskId", taskId);
        CollectClassFragment fragment = new CollectClassFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_collect_class_list, container, false);
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        assert getArguments() != null;
        CollectClassAdapter collectClassAdapter = new CollectClassAdapter(list,getArguments().getString("taskId"),getActivity());
        mBinding.recyclerView.setAdapter(collectClassAdapter);
        collectClassViewModel = ViewModelProviders.of(this).get( CollectClassViewModel.class);
//        collectClassViewModel.getCollectClass(getArguments().getString("type"),"",getArguments().getString("taskId"));

        collectClassViewModel.getmItems().observe(this, classes -> {
            collectClassAdapter.setList(classes);
            collectClassAdapter.notifyDataSetChanged();
            mBinding.refreshLayout.finishRefresh();
        } );

        collectClassViewModel.getmFaceDesc().observe(this, s -> mBinding.setStatus(s) );

        mBinding.refreshLayout.setRefreshFooter(new BallPulseFooter( Objects.requireNonNull( getActivity() ) ).setSpinnerStyle(SpinnerStyle.Scale));
        mBinding.refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        mBinding.refreshLayout.setEnableLoadMore(false);
        mBinding.refreshLayout.setOnRefreshListener( refreshlayout -> {
            assert getArguments() != null;
            collectClassViewModel.getCollectClass(getArguments().getString("type"),"",getArguments().getString("taskId"));
        } );
        mBinding.refreshLayout.setOnLoadMoreListener( refreshlayout -> {

        } );



        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        assert getArguments() != null;
        collectClassViewModel.getCollectClass(getArguments().getString("type"),"",getArguments().getString("taskId"));
    }
}
